package com.bw.echardemo.controller;

import com.bw.echardemo.pojo.TbRegister;
import com.bw.echardemo.service.TbRegisterService;
import excel.ExcelUtils;
import excel.ImportRspInfo;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("dao")
public class DaoController
{
    @Autowired
    TbRegisterService tbRegisterService;

    @RequestMapping("daoru")
    public String  parseSheet() throws Exception {
        // 1.获取源文件
        Workbook wb = WorkbookFactory.create(new FileInputStream("E:\\wangzhandata.xlsx"));
        // 2.获取sheet0导入
        Sheet sheet = wb.getSheetAt(0);
        // 3.生成VO实体
        ImportRspInfo<TbRegister> list = ExcelUtils.parseSheet(TbRegister.class, EvaluateVerifyBuilder2.getInstance(), sheet, 3, 2);

        boolean f = tbRegisterService.addAll(list);
        if (f) {
            return "index";
        }else {
            return "error" ;
        }
    }

    @RequestMapping("getData")
    public Map<String,Object> getData(){

        //定义Map集合返回前台
        Map<String, Object> m = new HashMap<>();
        //1查一个星期的日期字符串数组,以此为查询一个星期的时间条件
        List<String> dateStrs = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<7;i++){
            calendar.add(Calendar.DATE,-1);
            String date = simp.format(calendar.getTime());
            dateStrs.add(date);
        }

        //将当下查询的所有日期放入map中展示给前台
        m.put("dateStrs", dateStrs);

        //2查爱心人士近一个星期的注册数据
        m.put("lover", tbRegisterService.findByDateL(dateStrs));

        //3查贫困户近一个星期的注册数据
        m.put("poor", tbRegisterService.findByDateP(dateStrs));

        //4查管理员近一个星期的注册数据
        m.put("admin", tbRegisterService.findByDateA(dateStrs));
        //查性别比例
        m.put("sexData",tbRegisterService.findBySex());

        return m ;

    }



}
